<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Spatie\Permission\Models\Permission;

class IndexController extends Controller
{
    /**
     * 后台首页
     */
    public function index() {
        return view('index');
    }
    /**
     * 退出
     */
    public function logout() {
        Auth::logout();
    }
    /**
     * 初始化菜单
     */
    public function init() {
        //获取当前登录用户权限ID组
        $user = Auth::user();
        $permission = $user->getAllPermissions()->pluck('id')->toArray();
        //组装数据
        $menuArr['homeInfo'] = [
            'title' => '首页',
            'href' => 'page/welcome-2.html'
        ];
        $menuArr['logoInfo'] = [
            'title' => 'DT订货系统',
            'image' => 'images/logo.png',
        ];
        $menu = Permission::all()->toArray();
        foreach ($menu as $v) {
            if ($v['pid'] == 0 && in_array($v['id'], $permission)) {
                //获取二级菜单
                $child = [];
                foreach ($menu as $val) {
                    if ($val['pid'] == $v['id'] && in_array($val['id'], $permission)) {
                        array_push($child, [
                            'title' => $val['name'],
                            'href' => $val['router'],
                            'icon' => $val['icon'],
                            'target' => '_self',
                        ]);
                    }
                }
                $menuArr['menuInfo'][] = [
                    'title' => $v['name'],
                    'href' => '',
                    'icon' => $v['icon'],
                    'target' => '_self',
                    'child' => $child,
                ];
            }
        }
        return json_encode($menuArr);
    }
}
